<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px;width: 100%"
  >
    <!--左边-->
    <el-col>
      <span>{{ treeData.name }}</span>
    </el-col>
    <!--右边-->
    <el-col :span="10">
      <el-row type="flex" justify="end">
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <!-- 放置下拉菜单 -->
          <el-dropdown>
            <!-- 内容 -->
            <span>操作 <i class="el-icon-arrow-down"/> </span>
            <!-- 具名插槽 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$emit('openDialog',treeData.id)">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="$emit('editDialog',treeData.id)">编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepart">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { deleteDepartmentApi } from '@/api/department'

export default {
  name: 'TreeTools',
  props: {
    treeData: {
      type: Object,
      default: () => {
      }
    },
    // 用于标识当前组件是不是根组件
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async delDepart() {
      console.log('要删除频道id为' + this.treeData.id)
      try {
        await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 删除部门
        await deleteDepartmentApi(this.treeData.id)
        // 提示用户
        this.$message.success('删除成功')
        // 重新加载数据
        this.$emit('updateList')
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style scoped>

</style>
